package com.xinzhi.yjyz.comtroller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinzhi.entity.Keeper;
import com.xinzhi.util.R;
import com.xinzhi.yjyz.mapper.KeeperMapper;
import com.xinzhi.yjyz.service.IKeeprtService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class KeeperController {
    @Resource
    private IKeeprtService iKeeprtService;
    @Resource
    private KeeperMapper keeperMapper;


    @GetMapping("/sel")
    public R select() {
        List<Keeper> keepers = keeperMapper.selectList(null);
         keepers.forEach(System.out::println);
        return new R(200, "查询成功",keepers);
    }

    @PostMapping("/selphone/{phone}")
    public R selectphone(@PathVariable("phone") String phone) {
        if(phone.length()!=4&&phone!=null){
            return new R(200, "查询失败", "");
        }else {
            QueryWrapper<Keeper> keeperQueryWrapper = new QueryWrapper<>();
            keeperQueryWrapper.like("kds_phone", phone);
            List<Keeper> keepers=keeperMapper.selectList(keeperQueryWrapper);
            return new R(200, "查询成功", keepers);
        }
    }
    @PostMapping("/selname/{name}")
    public R selectname(@PathVariable("name") String name) {
        if (name!=null){
            QueryWrapper<Keeper> keeperQueryWrapper = new QueryWrapper<>();
            keeperQueryWrapper.like("kds_name", name);
            List<Keeper> keepers=keeperMapper.selectList(keeperQueryWrapper);
            keepers.forEach(System.out::println);
            return new R(200, "查询成功", keepers.get(2));
        }else {
            return new R(200, "查询失败", "");
        }
    }
    @PostMapping("/selnumber/{number}")
    public R selectnumber(@PathVariable String number) {
         if (number!=null){
             QueryWrapper<Keeper> keeperQueryWrapper = new QueryWrapper<>();
             keeperQueryWrapper.like("kds_number", number);
             List<Keeper> keepers=keeperMapper.selectList(keeperQueryWrapper);
             return new R(200, "查询成功", keepers);
         }else {
             return new R(401,"查询失败","");
         }
    }
    @PostMapping("/insphone/{phone}")
    public R insertphone(@PathVariable String phone) {
        if(phone.length()!=4&&phone!=null){
            return new R(200, "修改失败", "请输入手机号后四位");
        }else {
            QueryWrapper<Keeper> keeperQueryWrapper = new QueryWrapper<>();
            keeperQueryWrapper.like("kds_phone", phone);
            Keeper keeper = new Keeper();
            keeper.setKds_state("1");
            int update = keeperMapper.update(keeper, keeperQueryWrapper);
            return new R(200, "修改成功", update);
        }
    }
    @PostMapping("/insname/{name}")
    public R insertname(@PathVariable String name) {
        if (name!=null){
            QueryWrapper<Keeper> keeperQueryWrapper = new QueryWrapper<>();
            keeperQueryWrapper.like("kds_name",name);
            Keeper keeper=new Keeper();
            keeper.setKds_state("1");
            int update= keeperMapper.update(keeper,keeperQueryWrapper);
            return new R(200, "修改成功", update);
        }else {
            return new R(200, "修改失败", "");
        }

    }
    @PostMapping("/insnumber/{number}")
    public R insertnumber(@PathVariable String number) {
        if (number!=null){
            QueryWrapper<Keeper> keeperQueryWrapper = new QueryWrapper<>();
            keeperQueryWrapper.like("kds_number",number);
            Keeper keeper=new Keeper();
            keeper.setKds_state("1");
            int update= keeperMapper.update(keeper,keeperQueryWrapper);
            return new R(200, "修改成功", update);
        }else {
            return new R(200, "修改失败", "");
        }
    }
   @PostMapping("/insert")
   @ResponseBody
    public R insert(Keeper keeper){
       Keeper keeper1=new Keeper();
       keeper1.setKds_phone(keeper.getKds_phone());
       keeper1.setKds_name(keeper.getKds_name());
       keeper1.setKds_number(keeper.getKds_number());
       keeper1.setKds_waybill(keeper.getKds_waybill());
       keeper1.setKds_kdname(keeper.getKds_kdname());
       keeper1.setKds_dname(keeper.getKds_dname());
       keeper1.setKds_faddress(keeper.getKds_faddress());
       keeper1.setKds_saddress(keeper.getKds_saddress());
        keeperMapper.insert(keeper1);
        return new R(200,"增加成功",keeper);
   }

}